package com.lkei.controller;

import com.alibaba.fastjson.JSONObject;
import com.lkei.dto.Parameter;
import com.lkei.dto.Result;
import com.lkei.entity.Major;
import com.lkei.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("major")
public class MajorController {

    @Autowired
    private MajorService majorService;

    @RequestMapping("findByDepartmentID")
    public Result findMajorByDepartmentID(@RequestBody Parameter data){
        /**
         * TODO:记得添加JWT请求验证
         */
        int departmentID = Integer.parseInt(data.getData().toString());
        List<Major> majors = majorService.findMajorByDepartmentID(departmentID);
        return Result.SUCCESS.setNewData(majors);
    }

    @CrossOrigin
    @RequestMapping("findByStudentID")
    public Result findByStudentID(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        long StudentID = Long.valueOf(map.get("account").toString()).longValue();
        List<Major> major = majorService.findByStudentID(StudentID);
        Integer major_id = 0;
        for ( Major major1:
             major) {
             major_id = major1.getId();
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("major",major);
        hashMap.put("major_id",major_id);
        return Result.SUCCESS.setNewData(hashMap);
    }
}
